//
//  WXPOrderDetailHeaderView.m
//  MexicoLoan
//
//  Created by wxp on 2025/3/18.
//

#import "WXPOrderDetailHeaderView.h"

@implementation WXPOrderDetailHeaderView

#pragma mark - lazy
- (WXPOrderDetailTopMoneyView *)topMoneyView{
    if (!_topMoneyView) {
        _topMoneyView = [WXPOrderDetailTopMoneyView new];
    }
    return _topMoneyView;
}
- (UIButton *)btnRepayment{
    if (!_btnRepayment) {
        _btnRepayment = [UIButton new];
        _btnRepayment.widthHeight = XY(SCREEN_WIDTH-LEFT_MARDIN*2, BUTTON_HEIGHT);
        [GlobalMethod setRoundView:_btnRepayment color:[UIColor clearColor] numRound:BUTTON_HEIGHT/2 width:0];
        [_btnRepayment setTitleColor:COLOR_BLACK forState:UIControlStateNormal];
        _btnRepayment.titleLabel.fontNum = 14;
        [_btnRepayment setTitle:@"Ir a reembolsar" forState:UIControlStateNormal];
        _btnRepayment.backgroundColor = COLOR_PRIMARY;
        _btnRepayment.tag = 1;
        [_btnRepayment addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnRepayment;
}
- (UIButton *)btnRostpone{
    if (!_btnRostpone) {
        _btnRostpone = [UIButton new];
        _btnRostpone.widthHeight = XY(SCREEN_WIDTH-LEFT_MARDIN*2, BUTTON_HEIGHT);
        [GlobalMethod setRoundView:_btnRostpone color:[UIColor clearColor] numRound:BUTTON_HEIGHT/2 width:0];
        [_btnRostpone setTitleColor:COLOR_BLACK forState:UIControlStateNormal];
        _btnRostpone.titleLabel.fontNum = 14;
        [_btnRostpone setTitle:@"Ir a pedir prórroga de periodo" forState:UIControlStateNormal];
        _btnRostpone.backgroundColor = COLOR_PRIMARY;
        _btnRostpone.tag = 2;
        [_btnRostpone addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnRostpone;
}


#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.widthHeight = XY(SCREEN_WIDTH, 0);
        self.backgroundColor = [UIColor clearColor];
        [self addSubView];
    }
    return self;
}

- (void)addSubView{
    [self addSubview:self.topMoneyView];
    [self addSubview:self.btnRostpone];
    [self addSubview:self.btnRepayment];
    [self resetView:nil];
}

#pragma mark - 点击事件
- (void)btnClick:(UIButton *)sender{
    [NSObject clickFeedback];
    [sender addClickAnimate];
    switch (sender.tag) {
        case 1: // 还款
        {
            if(self.blockRepayment){
                self.blockRepayment();
            }
        }
            break;
        case 2: // 延期
        {
            if(self.blockDeferRepayment){
                self.blockDeferRepayment();
            }
        }
            break;
        default:
            break;
    }
}

#pragma mark - 刷新View
- (void)resetView:(ModelOrderDetail *)model{
    [self.topMoneyView resetView:model];
    self.topMoneyView.centerXTop = XY(self.width/2, 0);
    
    if ([strDotF(model.delayStatus) intValue] == 1){
        // delayStatus字段为1，代表可展期，显示展期按钮
        self.btnRostpone.hidden = false;
    }else{
        self.btnRostpone.hidden = true;
    }
    self.btnRostpone.centerXTop = XY(self.width/2, self.topMoneyView.bottom+LEFT_MARDIN);
    
    self.btnRepayment.hidden = !([model.status integerValue] == 170 || [model.status integerValue] == 180);
    self.btnRepayment.centerXTop = XY(self.width/2, self.btnRostpone.bottom+LEFT_MARDIN);
    if(self.btnRostpone.hidden == true){
        self.btnRepayment.centerXTop = XY(self.width/2, self.topMoneyView.bottom+LEFT_MARDIN);
    }
    if (self.btnRepayment.hidden) {
        self.height = self.topMoneyView.bottom+LEFT_MARDIN;
    } else {
        self.height = self.btnRepayment.bottom+LEFT_MARDIN;
    }
}

@end
